package com.dycc.main_componet.view.impl.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.dycc.libary.base.BaseFragment;
import com.dycc.libary.config.CustomLinearLayoutManager;
import com.dycc.libary.http.commonbean.attach.PicBean;
import com.dycc.libary.pull_refresh.container.DefaultFooter;
import com.dycc.libary.pull_refresh.container.DefaultHeader;
import com.dycc.libary.pull_refresh.widget.SpringView;
import com.dycc.libary.view.DividerItemDecoration;
import com.dycc.libary.view.xbanner.XBanner;
import com.dycc.libary.view.xbanner.transformers.Transformer;
import com.dycc.main_componet.R;
import com.dycc.main_componet.R2;
import com.dycc.main_componet.adapter.Rc_MenuAdapter;
import com.dycc.main_componet.adapter.Rc_NewsAdapter;
import com.dycc.main_componet.adapter.bean.Rc_Menu;
import com.dycc.main_componet.adapter.bean.Rc_MixBean;
import com.dycc.main_componet.adapter.bean.Rc_News;
import com.dycc.main_componet.adapter.bean.Rc_OnlyPicBean;
import com.dycc.main_componet.adapter.bean.Rc_VideoBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 推荐页面
 */
@Route(path = "/main_c/fragment/Main_CRCFragment", name = "推荐")
public class Main_CRCFragment extends BaseFragment {
    List<String> images = new ArrayList<>();
    List<String> titles = new ArrayList<>();
    List<Rc_News> rc_news = new ArrayList<>();

    @BindView(R2.id.main_c_xbanner)
    XBanner mainCXbanner;
    @BindView(R2.id.mainfragment_springview)
    SpringView springView;

    Unbinder unbinder;

    @BindView(R2.id.main_crc_menu_recyclerview)
    RecyclerView recyclerMenuView;
    @BindView(R2.id.main_crc_recyclerview)
    RecyclerView mainCrcRecyclerview;
    Rc_MenuAdapter adapter;
    Rc_NewsAdapter newsAdapter;
    List<Rc_Menu> menus=new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main__cindex__rc;
    }


    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        images.add("http://pics.sc.chinaz.com/files/pic/pic9/201307/gpic205.jpg");
        images.add("http://pics.sc.chinaz.com/files/pic/pic9/201310/apic1610.jpg");
        images.add("http://pics.sc.chinaz.com/files/pic/pic9/201712/zzpic8996.jpg");
        images.add("http://pic1.sc.chinaz.com/Files/pic/pic9/201801/bpic5414_s.jpg");
        titles.add("图片1");
        titles.add("图片2");
        titles.add("图片3");
        titles.add("图片4");

        List<PicBean> picBeens1=new ArrayList<>();
        picBeens1.add(new PicBean("丰收的季节","http://img.ivsky.com/img/tupian/pre/201608/23/nongzuowu_fengshou-002.jpg"));
        rc_news.add(new Rc_News<Rc_MixBean>(new Rc_MixBean(picBeens1,"丰收的季节"
                ,Rc_News.NEWS_MIX,"2017-06-03","22"),Rc_News.NEWS_MIX));

        List<PicBean> picBeens3=new ArrayList<>();
        picBeens3.add(new PicBean("国家统计局:2017年全国大麦产量2.5亿吨，增长率0.35%","http://img.ivsky.com/img/tupian/pre/201608/23/nongzuowu_fengshou-007.jpg"));
        picBeens3.add(new PicBean("国家统计局:2017年全国大麦产量2.5亿吨，增长率0.35%","http://img.ivsky.com/img/tupian/pre/201608/23/nongzuowu_fengshou-010.jpg"));
        picBeens3.add(new PicBean("国家统计局:2017年全国大麦产量2.5亿吨，增长率0.35%","http://img.ivsky.com/img/tupian/pre/201401/27/maizi-018.jpg"));

        rc_news.add(new Rc_News(new Rc_OnlyPicBean(picBeens3,"丰收的季节"
                ,Rc_News.NEWS_ONLYPIC,"2017-06-03","22"),Rc_News.NEWS_ONLYPIC));


        List<PicBean> picBeens2=new ArrayList<>();
        picBeens2.add(new PicBean("一望无际的麦田","http://img.ivsky.com/img/tupian/pre/201608/23/nongzuowu_fengshou-003.jpg"));
        rc_news.add(new Rc_News<Rc_MixBean>(new Rc_MixBean(picBeens2,"一望无际的麦田"
                ,Rc_News.NEWS_MIX,"2017-06-03","22"),Rc_News.NEWS_MIX));

        rc_news.add(new Rc_News<Rc_MixBean>(new Rc_MixBean(picBeens1,"丰收的季节"
                ,Rc_News.NEWS_MIX,"2017-06-03","22"),Rc_News.NEWS_MIX));

        rc_news.add(new Rc_News<Rc_VideoBean>(new Rc_VideoBean("http://video.jiecao.fm/5/1/%E8%87%AA%E5%8F%96%E5%85%B6%E8%BE%B1.mp4","丰收时节雨纷纷",Rc_News.NEWS_VIDEO,"",""
                ,"http://img4.jiecaojingxuan.com/2016/5/1/3430ec64-e6a7-4d8e-b044-9d408e075b7c.jpg"),Rc_News.NEWS_VIDEO));
        rc_news.add(new Rc_News<Rc_MixBean>(new Rc_MixBean(picBeens2,"一望无际的麦田"
                ,Rc_News.NEWS_MIX,"2017-06-03","22"),Rc_News.NEWS_MIX));
        rc_news.add(new Rc_News(new Rc_OnlyPicBean(picBeens3,"丰收的季节"
                ,Rc_News.NEWS_ONLYPIC,"2017-06-03","22"),Rc_News.NEWS_ONLYPIC));


        newsAdapter=new Rc_NewsAdapter(rc_news,getActivity());
        CustomLinearLayoutManager manager = new CustomLinearLayoutManager(getActivity());
        manager.setScrollEnabled(true);//滑动监听
        mainCrcRecyclerview.setLayoutManager(manager);
        mainCrcRecyclerview.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL_LIST));
        newsAdapter.isFirstOnly(false);
        mainCrcRecyclerview.setAdapter(newsAdapter);
        mainCrcRecyclerview.setNestedScrollingEnabled(false);


        mainCXbanner.setData(images, titles);
        mainCXbanner.setmAdapter(new XBanner.XBannerAdapter() {

            @Override
            public void loadBanner(XBanner banner, Object model, View view, int position) {
                Glide.with(getActivity()).load(images.get(position))
                        .placeholder(R.drawable.pic_defaultholder).into((ImageView) view);
            }
        });

        mainCXbanner.setPageTransformer(Transformer.Default);
        mainCXbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
            @Override
            public void onItemClick(XBanner banner, int position) {
            }
        });
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                    }
                }, 2000);
            }

            @Override
            public void onLoadmore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                    }
                }, 2000);
            }
        });
        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerMenuView.setLayoutManager(linearLayoutManager);
        menus.add(new Rc_Menu(R.drawable.image_rc_menu_01,"政策法规"));
        menus.add(new Rc_Menu(R.drawable.image_rc_menu_02,"价格动态"));
        menus.add(new Rc_Menu(R.drawable.image_rc_menu_03,"天气预警"));
        menus.add(new Rc_Menu(R.drawable.image_rc_menu_04,"农业资讯"));
        menus.add(new Rc_Menu(R.drawable.image_rc_menu_05,"质量追溯"));
        menus.add(new Rc_Menu(R.drawable.image_rc_menu_06,"气象旬报"));
        adapter=new Rc_MenuAdapter(getActivity(),menus);
        recyclerMenuView.setAdapter(adapter);
    }

    @Override
    protected void initData() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
